import Baby
import Event

class BController {
  def babyService
  def authenticateService
  def index = {
    redirect(controller:"login", action:"auth")
  }
  def show = {
    def person = null
    if(authenticateService.isLoggedIn()){
       person =   authenticateService.userDomain()
    }

    if(params.id){
      def baby = Baby.get(params.id)
      if(baby.person.id == person?.id){
        return [baby: baby, isManager:true]
      } else {
        return render(view:'visit', model:[baby: baby])
      }
    }
  }

  def saveGrowing = {
    def person = null
    if(authenticateService.isLoggedIn()){
       person =   authenticateService.userDomain()
    } else {
      redirect controller:'login'
      return
    }

    def grow = new Growing()
    grow.properties = params
    grow.creator = person
    if(!grow.save()){
      flash.message = grow.errors.toString()
    }

    def ev = new Event(
            baby: grow.baby,
            title: grow.content,
            param1: grow.id.toString(),
            eventType: Event.EVENT_TYPE_POST_GROW

    )
    ev.save()
    redirect(controller:'b', action:'show', id:grow.baby.id)
  }

  def tasks = {
    def baby = Baby.get(params.id)
    def stage = babyService.getStage(baby)
    def sql = "from Plan as t where t.startWith <=:stage and t.endWith >=:stage and t.planType =:planType and t.published is true"
    def planList = Plan.findAll (sql, [stage:stage, planType:Plan.PLAN_TYPE_SHOULD_DO])
    Collections.sort(planList)
    [planList:planList, baby:baby] 
  }

  def news = {
    def baby = getBaby(params)
    def entryList = Entry.findAll()
    [baby:baby, entryList:entryList]
  }

  def study = {
    def entries = Entry.findAll()
    def entry = entries[0]
    [entry:entry]
  }

  def favorite = {
    def baby = getBaby(params)
    params.max = params.max ? params.max.toInteger() : 5
    params.offset = params.offset ? params.offset.toInteger() : 0
    def experienceList = Experience.list(sort:'dateCreated', order:'desc', max:params.max, offset:params.offset)
    def total =  Experience.count()

    [baby:baby, experienceList:experienceList, total:total]    
  }

  def experience = {
    def baby = getBaby(params)
    params.max = params.max ? params.max.toInteger() : 5
    params.offset = params.offset ? params.offset.toInteger() : 0
    def experienceList = []
    def total = 0
    if(babyService.isManager()){
      experienceList = Experience.list(sort:'dateCreated', order:'desc', max:params.max, offset:params.offset)
      total =  Experience.count()
    } else {
      experienceList = Experience.findAllByBaby(baby, [sort:'dateCreated', order:'desc', max:params.max, offset:params.offset])
      total =  Experience.countByBaby(baby)
    }


    [baby:baby, experienceList:experienceList, total:total]    
  }

  def lessons = {
    def baby = Baby.get(params.id)
    def stage = babyService.getStage(baby)
    def sql = "from Plan as t where t.startWith <=:stage and t.endWith >=:stage and t.planType =:planType"
    def planList = Plan.findAll (sql, [stage:stage, planType:Plan.PLAN_TYPE_SHOULD_CARE])
    Collections.sort(planList)
    [planList:planList, baby:baby]     
  }

  def milestone = {
    def baby = Baby.get(params.id)
    def stage = babyService.getStage(baby)
    [stage:stage]
  }

  def events = {
    def baby = getBaby(params)
    def events = Event.list(sort:'dateCreated', order:'desc', max:15)
    [baby:baby, eventList:events]
  }

  def edit = {
    def baby = Baby.get(1)
    [baby:baby]
  }

  def getBaby(params){
    def baby = null
    if(params.id){
      baby = Baby.get(params.id)
    } else {
      baby = Baby.get(1)
    }
    return baby
  }

  def ajaxdeletegrowing = {
    //todo add permision controll
    def growingInstance = Growing.get(params.id)
    if (growingInstance) {
      try {
        growingInstance.delete()
        render "1"
      }
      catch (org.springframework.dao.DataIntegrityViolationException e) {
        render "0"
      }
    }
    else {
      render "-2"
    }        
  }

}
